package com.sky.service.impl;

import com.github.pagehelper.Constant;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.jsonwebtoken.lang.Collections;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @Author by 孙有泽
 * @Date 2025/3/3 14:18
 * @Description 菜品 serviceImple
 */
@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Override
    @Transactional
    public void save(DishDTO dishDto) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDto,dish);
        dish.setStatus(0);
        dishMapper.insert(dish);
        List<DishFlavor> flavors = dishDto.getFlavors();

        if(!Collections.isEmpty(flavors)) {
            List<DishFlavor> flavorList= flavors.stream().map((item) -> {
                        item.setDishId(dish.getId());
                        return item;
            }
            ).collect(Collectors.toList());
            dishFlavorMapper.insertBatch(flavorList);
        }
    }

    @Override
    public PageResult list(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<DishVO> page =dishMapper.list(dishPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    @Transactional
    public void delete(List<Long> ids) {
        for (Long id:ids){
            //查询菜品是启用状态 不能删除
            Dish dish=dishMapper.selectStatusById(id);
            if(dish.getStatus()==1){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        //如果菜品被套餐关联也不能删除
        Integer count=setmealDishMapper.selectByDishIds(ids);
        if(count>0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //删除菜品
        for(Long id:ids){
            //删除菜品
            dishMapper.deleteById(id);
            //删除关联的口味
            dishFlavorMapper.deleteByDishId(id);
        }
    }

    @Override
    public void updateStatus(Integer status, Long id) {
        Dish dish = new Dish();
        dish.setStatus(status);
        dish.setId(id);
        dishMapper.update(dish);
    }

    @Override
    @Transactional
    public DishVO getById(Long id) {
        Dish dish =dishMapper.getById(id);
        List<DishFlavor>  dishFlavors= dishFlavorMapper.getFlavors(id);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavors);
        return dishVO;
    }

    @Override
    public List<DishVO> selectDishByCategoryId(Long categoryId) {
        List<DishVO> dishVOS = dishMapper.selectDishByCategoryId(categoryId);
        return dishVOS;
    }

    @Override
    @Transactional
    public void update(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);
        //根据id删除口味
        dishFlavorMapper.deleteByDishId(dish.getId());
        if(!Collections.isEmpty(dishDTO.getFlavors())) {
            List<DishFlavor> flavorList = dishDTO.getFlavors().stream().map(item -> {
                item.setDishId(dish.getId());
                return item;
            }).collect(Collectors.toList());
            dishFlavorMapper.insertBatch(flavorList);
        }
    }

    @Override
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.listDish(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getFlavors(d.getId());
            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
}
